package com.architecture.core.web.actions;

import com.architecture.core.CoreCycleInterface;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * LoginAction: Administrator
 * Date: 12-5-7
 * Time: 下午2:29
 * To change this template use File | Settings | File Templates.
 */
@Scope("request")
@ParentPackage("struts-default")
@Results({
        @Result(name = "success", location = "result/success.jsp"),
        @Result(name = "input", location = "result/input.jsp"),
        @Result(name = "error", location = "result/error.jsp"),
        @Result(name = "login", location = "result/login.jsp"),
        @Result(name = "none", location = "result/none.jsp")
})
public abstract class AbstractAction extends ActionSupport implements CoreCycleInterface, ApplicationAware, SessionAware, RequestAware {

    protected final Logger logger = Logger.getLogger(this.getClass());

    private Map<String, Object> appMap = new HashMap<String, Object>();

    private Map<String, Object> sessionMap = new HashMap<String, Object>();

    private Map<String, Object> requestMap = new HashMap<String, Object>();

    protected static final String CREATE_SUCCESS = "create_success";
    protected static final String UPDATE_SUCCESS = "update_success";
    protected static final String DELETE_SUCCESS = "delete_success";
    protected static final String QUERY_SUCCESS = "query_success";
    protected static final String ACTIVE_SUCCESS = "active_success";
    protected static final String INACTIVE_SUCCESS = "inactive_success";
    protected static final String CREATE_ERROR = "create_error";
    protected static final String UPDATE_ERROR = "update_error";
    protected static final String DELETE_ERROR = "delete_error";
    protected static final String QUERY_ERROR = "query_error";
    protected static final String ACTIVE_ERROR = "active_error";
    protected static final String INACTIVE_ERROR = "inactive_error";

    protected static final String LIST = "list";
    protected static final String DETAIL = "detail";
    protected static final String ERROR = "error";

    protected static final String URL = "url";

    //内存中关键字
    protected static final String CURRENT_ID = "currentId";
    protected static final String USERNAME = "username";
    protected static final String IS_ADMIN = "isAdmin";

    //用于控制整个数据是否可用,进而控制整个页面
    private boolean disabledHead;
    private boolean showHead = true;

    private boolean disabledNav;
    private boolean showNav = true;

    private boolean disabledBody;
    private boolean showBody = true;

    private boolean disabledFoot;
    private boolean showFoot = true;

    @Override
    public void setSession(Map<String, Object> session) {
        this.sessionMap = session;
    }

    @Override
    public void setRequest(Map<String, Object> request) {
        this.requestMap = request;
    }

    @Override
    public void setApplication(Map<String, Object> application) {
        this.appMap = application;
    }

    protected Map<String, Object> getAppMap() {
        return appMap;
    }

    /**
     * 存放在session中的信息.
     * currentId    当前登录用户的ID
     * username     当前用户的用户名
     * grp          当前用户组号
     * userLevel    当前用户级别
     * userType     当前用户类型
     * state        当前用户状态：1.查看；2.新增；3.编辑；
     * @return
     */
    protected Map<String, Object> getSessionMap() {
        return sessionMap;
    }

    /**
     * 存放在request中信息
     * opera        当前操作
     * @return
     */
    protected Map<String, Object> getRequestMap() {
        return requestMap;
    }

    public boolean isAdmin() {

        if(null != this.sessionMap && null != this.sessionMap.get("isAdmin")){
             return (Boolean)this.sessionMap.get("isAdmin");
        }else{
            return false;
        }
    }

    @Override
    public void init() {
    }

    @Override
    public void destroy() {
    }

    public boolean isDisabledBody() {
        return disabledBody;
    }

    public void setDisabledBody(boolean disabledBody) {
        this.disabledBody = disabledBody;
    }

    public boolean isShowBody() {
        return showBody;
    }

    public void setShowBody(boolean showBody) {
        this.showBody = showBody;
    }

    public boolean isDisabledHead() {
        return disabledHead;
    }

    public void setDisabledHead(boolean disabledHead) {
        this.disabledHead = disabledHead;
    }

    public boolean isShowHead() {
        return showHead;
    }

    public void setShowHead(boolean showHead) {
        this.showHead = showHead;
    }

    public boolean isDisabledNav() {
        return disabledNav;
    }

    public void setDisabledNav(boolean disabledNav) {
        this.disabledNav = disabledNav;
    }

    public boolean isShowNav() {
        return showNav;
    }

    public void setShowNav(boolean showNav) {
        this.showNav = showNav;
    }

    public boolean isDisabledFoot() {
        return disabledFoot;
    }

    public void setDisabledFoot(boolean disabledFoot) {
        this.disabledFoot = disabledFoot;
    }

    public boolean isShowFoot() {
        return showFoot;
    }

    public void setShowFoot(boolean showFoot) {
        this.showFoot = showFoot;
    }

}
